﻿using System.Collections.Generic;
using System.Linq;
using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.BusinessEntities.Forecast.Xml;
using GC_FFW.ForecastWriter.Abstractions.Adapters;
using GC_FFW.ForecastWriter.Abstractions.Interfaces.Helpers;
using GC_FFW.ForecastWriter.Common;

namespace GC_FFW.ForecastWriter.Adapters.TeamMatch.Introduction
{
	public class AboutMatchProposalAdapter : ProposalBaseAdapter
	{
		#region Constructors

		public AboutMatchProposalAdapter(IForecastData forecastData, Conditions conditions,
			IStringManager stringManager, IRandomizer randomizer)
			: base(forecastData, conditions, stringManager, randomizer) { }

		#endregion

		#region Overrides

		public override string GetText()
		{
			var proposal = GetProposal();
			if (proposal == null) return string.Empty;

			var result = proposal.Text ?? string.Empty;

			if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

			var dictionary = new Dictionary<string, string>();
			foreach (var keyword in proposal.KeywordList)
			{
				switch (keyword)
				{
					case Constants.HOME_TEAM_NAME_KEYWORD:

						dictionary.Add(
							Constants.HOME_TEAM_NAME_KEYWORD,
							ForecastData.HomeTeam != null && ForecastData.HomeTeam.Name != null
								? ForecastData.HomeTeam.Name
								: string.Empty);

						break;

					case Constants.AWAY_TEAM_NAME_KEYWORD:

						dictionary.Add(
							Constants.AWAY_TEAM_NAME_KEYWORD,
							ForecastData.AwayTeam != null && ForecastData.AwayTeam.Name != null
								? ForecastData.AwayTeam.Name
								: string.Empty);

						break;
				}
			}

			result = StringManager.ReplaceKeywords(result, dictionary);

			return result;
		}

		#endregion
	}
}
